package service;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import common.kit.PageSql;
import enums.RepaireType;
import model.Repaire;
import model.Staff;
import model.Station;

public class RepaireService extends BaseService {

	public Page<Record> getApiPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tr.id,tr.recorder,tr.startTime,tr.endTime,tr.type,tr.remark,tr.repairRemark,tr.needCheck,tr.needCalibration,tr.needRecheck,ts.name " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_repaire tr ") ;
	    pageSql.join(" t_servicer ts " , " ts.id = tr.serviserId ") ;
		pageSql.setEqualCondition("tr.stationId") ; //站点	
		pageSql.setNotLessThanCondition("tr.createTime", "startTime");
		pageSql.setNotMoreThanCondition("tr.createTime", "endTime");
	    pageSql.orderBy(" tr.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		for (Record record : page.getList()) {
			record.set("typeStr", RepaireType.getTitleByValue(record , "type"));
		}
	    return page ;
	}

	public Ret save(Repaire repaire , Staff staff , Station station) {
		repaire.setStationId(station.getId());
		repaire.setServiserId(staff.getServicerId());
		repaire.setRecorder(staff.getName());
		repaire.save();
	    return ok() ;
	}

	public Page<Record> getPage(Kv kv , Integer pageNumber , Integer pageSize) {
		String select = " tr.id,ts.name,tc.name AS companyName1,tr.startTime,tr.endTime,tr.type,tr.remark,tr.repairRemark,tr.needCheck,tr.needCalibration,tr.needRecheck,tsr.name AS servicerName,tr.recorder,tr.createTime " ;
	    PageSql pageSql = PageSql.bySelect( kv , select ) ;
	    pageSql.from(" t_repaire tr ") ;
	    pageSql.join(" t_station ts " , " ts.id = tr.stationId ") ;
	    pageSql.join(" t_servicer tsr " , " tsr.id = tr.serviserId ") ;
	    pageSql.join(" t_company tc " , " tc.id = ts.companyId ") ;
		pageSql.setNotLessThanCondition("tr.createTime","tr.createTime.start") ; //创建时间
		pageSql.setNotMoreThanCondition("tr.createTime","tr.createTime.end") ; //创建时间
		pageSql.setEqualCondition("tr.stationId") ; //站点	 
		pageSql.setEqualCondition("tr.type") ; //故障类型	 
		pageSql.setLikeCondition("ts.name") ; //点位名称	 
		pageSql.setLikeCondition("tsr.name") ; //运维公司	 
		pageSql.setLikeCondition("tc.name") ; //企业名称	 
	    pageSql.orderBy(" tr.createTime DESC ");
		Page<Record> page = pageSql.paginate( pageNumber , pageSize) ;
		for (Record record : page.getList()) {
			record.set("typeStr", RepaireType.getTitleByValue(record , "type"));
		}
	    return page ;
	}
}
